package com.inifitness.admin.task;

import com.aliyun.sdk.service.dysmsapi20170525.models.QuerySmsTemplateListResponseBody;
import com.aliyun.sdk.service.dysmsapi20170525.models.QuerySmsTemplateListResponseBody.SmsTemplateList;
import com.inifitness.sms.service.AliCloudSmsService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

/**
 * Websocket心跳检测
 *
 * @author sunjinfeng
 * @version 1.0
 * @since 2024/1/19 16:58
 */
@Component
@Log4j2
@RequiredArgsConstructor
public class AliCloudSmsQueryTask {

  private static final Integer PAGE_SIZE = 50;
  private final AliCloudSmsService aliCloudSmsService;

  /**
   * WebSocket 心跳检测-团课屏
   */
  @XxlJob("checkSmsTemplate")
  public void checkSmsTemplate() {
    QuerySmsTemplateListResponseBody responseBody = aliCloudSmsService.querySmsTemplateList(1,
        PAGE_SIZE);
    Long total = responseBody.getTotalCount();
    Integer page = 0;
    Long totalPage = (total + PAGE_SIZE - 1) / PAGE_SIZE;
    log.info("当前模板共:{}条，{}页", total, totalPage);
    do {
      page++;
      responseBody = aliCloudSmsService.querySmsTemplateList(page, PAGE_SIZE);
      for (SmsTemplateList template : responseBody.getSmsTemplateList()) {
        //TODO:数据入库
        log.info("模板编码:{},模板名称:{},模板审核状态:{},模板内容:{},原因:{}",
            template.getTemplateCode(), template.getTemplateName(), template.getAuditStatus(),
            template.getTemplateContent(), template.getReason().getRejectInfo());
      }
    } while (page < totalPage);
  }

  @XxlJob("executeSingleParam")
  public void executeSingleParam(){
    // 获取参数
    String param = XxlJobHelper.getJobParam();
    log.info("接收調度中心参数...[{}]",param);
    //控制台输出日志

    String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    log.info("任务:{} 开始执行...", method);

    try {
      //书写业务逻辑
      //TODO
      XxlJobHelper.log("任务:{} 执行成功...",method);
      XxlJobHelper.handleSuccess();
    } catch (Exception e) {
      log.error("任务:{} 执行失败 ...", method, e);
      XxlJobHelper.log("任务:{} 执行失败...",method);
      XxlJobHelper.handleFail();
    }
  }

  @XxlJob("executeMultiParam")
  public void executeMultiParam(){
    final int paramsSize = 2;
    String param = XxlJobHelper.getJobParam();
    String[] methodParams = param.split(",");
    if (methodParams.length < paramsSize){
      return;
    }
    log.info("执行参数1..[{}]", methodParams[0]);
    log.info("执行参数2..[{}]", methodParams[1]);

    String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    //控制台输出日志
    log.info("任务:{} 开始执行...",method);

    try {
      //书写业务逻辑
      //TODO
      XxlJobHelper.log("任务:{} 执行成功...",method);
      XxlJobHelper.handleSuccess();
    } catch (Exception e) {
      log.error("任务:{} 执行失败 ...", method, e);
      XxlJobHelper.log("任务:{} 执行失败...",method);
      XxlJobHelper.handleFail();
    }
  }
}
